package com.zxkj.commonpatrol.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;

import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.PlayVioceAdapt;
import com.zxkj.commonpatrol.databinding.ActivityPlayerVioceBinding;
import com.zxkj.commonpatrol.model.response.MediaInfoResponse;
import com.zxkj.commonpatrol.viewmodel.PlayerVioceViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Phaser;

public class PlayerVioceActivity extends BaseVmDataBindingActivity<ActivityPlayerVioceBinding, PlayerVioceViewModel> {

    private String data;
    private    List<MediaInfoResponse.MediaVoice>  mediaVoiceData;
    private MiniLoadingDialog loadingDialog;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_player_vioce;
    }

    @Override
    public int initVariableId() {
        return BR.playVioceViewModel;
    }


    @Override
    protected void initUI() {
        if (getIntent().hasExtra("data")){
            data=getIntent().getStringExtra("data");
            if (!TextUtils.isEmpty(data)){
                mediaVoiceData = new Gson().fromJson(data, new TypeToken<List<MediaInfoResponse.MediaVoice>>(){}.getType());

            }

        };
            binding.recy.setLayoutManager(new LinearLayoutManager(this));
         loadingDialog =new MiniLoadingDialog(this,"加載中...");
        loadingDialog.show();
       getData();

    }

    @Override
    protected void setListener() {
        super.setListener();
        binding.qkBack.setOnClickListener(v->{
            //删除文件数据并退出
            ActivityTool.getInstance().popOneActivity(this);

        });
    }

    private void getData() {
        //去下載
        if (mediaVoiceData!=null&&mediaVoiceData.size()>0) {
                Map<Integer, MediaInfoResponse.MediaVoice> integerMediaVoiceHashMap = new HashMap<>();
                for (int i = 0; i <mediaVoiceData.size() ; i++) {
                    integerMediaVoiceHashMap.put(i,mediaVoiceData.get(i));
                }

           viewModel.DownVicoe(integerMediaVoiceHashMap);

        }
    }

    @Override
    protected void initData() {

    }


    @Override
    protected void dataObserver() {
        super.dataObserver();

        viewModel.VicoeData.observe(this, new Observer<StateData<List<String>>>() {
            @Override
            public void onChanged(StateData<List<String>> listStateData) {
                if (listStateData.getStatus()== StateData.DataStatus.SUCCESS){
                    if ( loadingDialog!=null){
                        loadingDialog.dismiss();
                    }
                    if (listStateData.getData()!=null){
                        PlayVioceAdapt adapt =new PlayVioceAdapt(R.layout.item_vicoe_video,listStateData.getData(),getSupportFragmentManager());
                        binding.recy.setAdapter(adapt);
                    }


                }else{
                    if ( loadingDialog!=null){
                        loadingDialog.dismiss();
                    }
                }


            }
        });
    }
}